package com.xjscrm.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * DateFormatUtil
 */
public final class DateFormatUtil {

    private static Logger logger = LoggerFactory.getLogger(DateFormatUtil.class);

    private static DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    private static DateTimeFormatter formatDay = DateTimeFormatter.ofPattern("yyyyMMdd");
    private static DateTimeFormatter formatYmd = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 获取当天的日期字符串
     * @return yyyyMMdd
     */
    public static String getToDayStr(){
        try {
            LocalDate today = LocalDate.now();
            String dateStr = today.format(formatDay);
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 获取当天的日期字符串
     * @return yyyy-MM-dd
     */
    public static String getToDayFormatStr(){
        try {
            LocalDate today = LocalDate.now();
            String dateStr = today.format(formatYmd);
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 日期格式成日期字符串
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToStr(Date date){
        try {
            LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            String dateStr = localDate.format(format);
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 日期格式成日期字符串
     * @param date
     * @param formatTemplate
     * @return
     */
    public static String dateToStr(Date date,String formatTemplate){
        try {
            LocalDateTime localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
            String dateStr = localDate.format(DateTimeFormatter.ofPattern(formatTemplate));
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 时间戳格式化成日期字符串
     * @param time
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String timeToDateStr(int time){
        try {
            Date date = new Date((long)time*1000l);
            return dateToStr(date);
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 获取当天的时间字符串
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static  String getToDayTime() {
        try {
            LocalDateTime today = LocalDateTime.now();
            String dateStr = today.format(format);
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 将date日期转为yyyy-MM-dd 字符串
     * @param date
     * @return yyyy-MM-dd
     */
    public static String toStrYmd(Date date){
        try {
            LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            String dateStr = localDate.format(formatYmd);
            return dateStr;
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return "";
    }

    /**
     * 将dateStr转为时间戳
     * @param dateStr
     * @return yyyy-MM-dd
     */
    public static int ymdToTime(String dateStr){
        if (StringUtils.isBlank(dateStr)){
            return 0;
        }
        try {
            dateStr = StringUtils.join(dateStr," 00:00:00");
            LocalDateTime date = LocalDateTime.parse(dateStr, format);
            long time = date.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            return (int) (time/1000L);
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return 0;
    }

    /**
     * 字符串转时间戳(s)
     * @param val yyyy-MM-dd HH:mm:ss
     * @return s
     */
    public static int ymdhmsToTime(String val)  {
        if (StringUtils.isBlank(val)){
            return 0;
        }
        try {
            LocalDateTime date = LocalDateTime.parse(val, format);
            long time = date.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
            return (int) (time/1000L);
        } catch (Exception e) {
            logger.error("err:",e);
        }
        return 0;
    }

    /**
     * 字符串转 Date
     * @param str
     * @param formStr
     * @return
     */

    public static Date StringToDateFormat(String str,String formStr)  {
        Date date = null;
        try {
            SimpleDateFormat format = new SimpleDateFormat(formStr);
            date = format.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;

    }
}
